﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HZH_Controls.Forms;
using System.Data.SQLite;
using Petroleum.Tool;
using Petroleum.DB.Model;
using Petroleum.Model;
using Newtonsoft.Json;
using HZH_Controls;

namespace Petroleum.UC.Base
{
    public partial class ToolMenu : UserControl
    {
        FormMain main = null;
        //SQLiteHelper sqliteHelper = new SQLiteHelper(ResServer.DBURL);
        public SQLiteHelper sqliteHelper = new SQLiteHelper(ResServer.DBURL);

        public ToolMenu(FormMain main)
        {
            InitializeComponent();
            this.main = main;
        }
        public ToolMenu()
        {
            InitializeComponent();
        }
        private void btnClose_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);
        }

        private void btnMax_Click(object sender, EventArgs e)
        {
            if (main.WindowState == FormWindowState.Normal)
            {
                main.WindowState = FormWindowState.Maximized;
                return;
            }
            if (main.WindowState == FormWindowState.Maximized)
            {
                main.WindowState = FormWindowState.Normal;
                return;
            }
        }
        private void pictureBox1_Click(object sender, EventArgs e)
        {
            main.WindowState = FormWindowState.Minimized;
        }
        //private void btnsave_Click(object sender, EventArgs e)
        //{

        //    var casing = projects.Where(n => n.TableName == TableNames.CasingProgram.ToString()).FirstOrDefault();
        //    if (casing != null)
        //        SaveData(TableNames.CasingProgram.ToString(), JsonConvert.SerializeObject(Session.CasingProgram));
        //    else
        //        SaveData(TableNames.CasingProgram.ToString(), JsonConvert.SerializeObject(Session.CasingProgram), 1);

        //    if (WellTrajectory != null)
        //        SaveData(TableNames.WellTrajectory.ToString(), JsonConvert.SerializeObject(Session.WellTrajectory));
        //    else
        //        SaveData(TableNames.WellTrajectory.ToString(), JsonConvert.SerializeObject(Session.WellTrajectory), 1);

        //}
        private void btnexpect_Click(object sender, EventArgs e)
        {
            try
            {
                SessionDynamic session = new SessionDynamic()
                {
                    BasicInf = Session.BasicInf,
                    CasingProgram = Session.CasingProgram,
                    FormationPressure = Session.FormationPressure,
                    MPAList = Session.MPAList,
                    MPADetailList = Session.MPADetailList,
                    ProgresPlanList = Session.ProgresPlanList,
                    ProjectId = Session.ProjectId,
                    Team = Session.Team,
                    TemplateFile = Session.TemplateFile,
                    WellName = Session.WellName,
                    WellTrajectory = Session.WellTrajectory,
                    DepthRangeModelList = Session.DepthRangeModelList,
                    DepthWellModelList = Session.DepthWellModelList,
                    BitTypeDes = Session.BitTypeDes,
                    Hydraulic = Session.Hydraulic,
                    slurries = Session.slurries
                };
                if (string.IsNullOrEmpty(session.TemplateFile))
                {
                    //模板路径为空，请先创建项目
                    FrmTips.ShowTipsWarning(this.FindForm(), $"模板路径为空，请先创建项目并选择模板");
                    return;
                }
                DialogResult dr = folderBrowserDialog1.ShowDialog();
                ControlHelper.ThreadRunExt(this, () =>
                {
                    //是调用文件浏览器控件；          
                    if (dr == System.Windows.Forms.DialogResult.OK)
                    {
                        //是判断文件浏览器控件是否返回ok，即用户是否确定选择。如果确定选择，则弹出用户在文件浏览器中选择的路径：                
                        string filePath = folderBrowserDialog1.SelectedPath;
                        SpireDocUtil.CreateDoc(session, session.TemplateFile, filePath);
                        ControlHelper.ThreadInvokerControl(this, () =>
                        {
                            FrmTips.ShowTipsSuccess(new FormMain(), "文档生成成功");
                        });
                    }
                }, null, this);
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }
    }
}
